using UnityEngine;
using System.Collections;

public class HotAirWeapon : MonoBehaviour {

    public float m_RecoilForce = 5.0f;
    public float m_EnergyLevel = 100.0f;
    public float m_Distance = 10.0f;

    private float m_CurrentEnergyLevel = 0;
    private float m_EnergyDrainingSpeed = 1.0f;
    private float m_RegenerationSpeed = 2.0f;

    public float m_BlastForce = 7.5f;

    private Vector3 m_WeaponDirection;
   

    private GameObject player;
	void Start () 
    {
        player = GameObject.FindGameObjectWithTag("Player") as GameObject;

        m_CurrentEnergyLevel = m_EnergyLevel;
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (Input.GetMouseButton(1) && m_CurrentEnergyLevel > 0)
        {
           
                //PLAYER ADJUSTMENTS
                //create perpendicular line with only the z info from the player
                Vector3 playerTarget = new Vector3(Camera.mainCamera.transform.position.x, Camera.mainCamera.transform.position.y, player.transform.position.z);
                //find the distance between the player and the camera
                float distance = Vector3.Distance(Camera.mainCamera.transform.position, playerTarget);
                //find the mousepos in world space based on the distance removed from camera
                Vector3 mouseWP = Camera.mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Mathf.Abs(distance) - 0.4f));

                Vector3 max = new Vector3(player.transform.position.x, 20, player.transform.position.z);
                //create difference vector
                m_WeaponDirection = mouseWP - player.transform.position;

               // RaycastHit hit;

                if (Physics.Raycast(player.GetComponent<CharacterController>().transform.position, new Vector3(0, -1, 0), m_Distance))
                {
                    player.GetComponent<Pawn>().SetHoverForce(m_RecoilForce, -m_WeaponDirection);
                }
                else
                {
                    Vector3 hoverForce = new Vector3(-m_WeaponDirection.x, 0, -m_WeaponDirection.z);
                    player.GetComponent<Pawn>().SetHoverForce(m_RecoilForce, hoverForce);
                }

                player.GetComponent<Pawn>().SetBoolGravity(false);

            //when weapon is being used substract some energy to avoid continues shooting
            m_CurrentEnergyLevel -= m_EnergyDrainingSpeed * Time.deltaTime;
        }
        else
        {
            if (m_CurrentEnergyLevel < m_EnergyLevel)
            {
                m_CurrentEnergyLevel += Time.deltaTime * m_RegenerationSpeed;
            }

            player.GetComponent<Pawn>().SetBoolGravity(true);

            player.GetComponent<Pawn>().SetHoverForce(0, Vector3.zero);
        }

        //Debug.Log("Weapon energy is: " + m_CurrentEnergyLevel);
	}

    public float GetCurrentEnergy()
    {
        return m_CurrentEnergyLevel;
    }
}
